<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>Title</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <script src="${pageContext.request.contextPath}/js/jquery-2.1.0.js"></script>
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

</head>
<body>

<a class="btn btn-primary" href="${pageContext.request.contextPath}/useradd.jsp">增加</a>

<table class="table table-condensed">
    <tr>
        <th>id</th>
        <th>username</th>
        <th>gender</th>
        <th>registTime</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${pageInfo.list}" var="user">
        <tr>
            <td>${user.id}</td>
            <td>${user.username}</td>
            <td>${user.gender}</td>
            <td>
                <fmt:formatDate value="${user.registTime}" pattern="yyyy/MM/dd HH:mm:ss"/>
            </td>
            <td>
                <button type="button" class="btn btn-danger" onclick="delUser(${user.id})">删除</button>
                <a class="btn btn-primary" href="${pageContext.request.contextPath}/user/${user.id}">更新</a>
            </td>
        </tr>
    </c:forEach>
</table>
<ul class="pagination">
    <li>
        <a href="${pageContext.request.contextPath}/users">首页</a>
    </li>
    <li>
        <a href="${pageContext.request.contextPath}/users?pageNum=${pageInfo.prePage}">上一页</a>
    </li>
    <li>
        <a href="${pageContext.request.contextPath}/users?pageNum=${pageInfo.nextPage}">下一页</a>
    </li>
    <li>
        <a href="${pageContext.request.contextPath}/users?pageNum=${pageInfo.pages}">尾页</a>
    </li>
</ul>
<script>
    function delUser(id){
        $.ajax({
            url:"${pageContext.request.contextPath}/deleteUser/"+id,
            type:"delete",
            success:function(ret){// {"status":"ok"}
                if(ret.status=="ok"){
                    alert("删除成功");
                }else{
                    alert("删除失败");
                }
                // 访问查询所有
                location.href="${pageContext.request.contextPath}/users";
            }
        })
    }


</script>
</body>
</html>
